package com.lsm.sm.util;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.lsm.sm.entity.ProxyUser;
import com.lsm.sm.entity.User;
import com.opensymphony.xwork2.ActionContext;


public class WebUtil {
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getRequest(){
		return (Map<String, Object>)ActionContext.getContext().get( "request" );
	}
	
	public static Map<String, Object> getSession(){
		return ActionContext.getContext().getSession();
	}
	
	public static void addInSession( String key, Object obj ){
		getSession().put( key, obj );
	}
	
	public static HttpServletRequest getHttpRequest(){
		return ServletActionContext.getRequest();
	}
	
	public static HttpServletResponse getHttpResponse(){
		return ServletActionContext.getResponse();
	}
	
	public static ProxyUser getProxyUser(){
		return (ProxyUser)getSession().get( Const.USER );
	}
	
	public static User getUser(){
		return (User)getSession().get( Const.USER );
	}
	
	public static String dealException( Exception e ){
		String emsg = e.getMessage();
		int idx = emsg.indexOf( ':' );
		if( idx != -1 ){
			return emsg.substring( idx+1 );
		}else{
			return emsg;
		}
	}
	
	public static String dealRespsMsg( String rmsg ){
		int idx = rmsg.indexOf( ':' );
		if( idx != -1 ){
			return rmsg.substring( idx+1 );
		}else{
			return rmsg;
		}
	}
	
	public static ServletContext getServletContext(){
		return ServletActionContext.getServletContext();
	}
}
